import pymysql


# 获取数据库连接
def get_connection():
    host = '116.62.46.50'
    port = 3306
    user = 'godayou'
    password = '123456'
    database = 'maoyan'
    db = pymysql.connect(host, user, password, database, charset='utf8', port=port)
    return db


# 获取游标
def get_cursor(con):
    cursor = con.cursor()
    return cursor


# 关闭数据库连接
def close_connection(con):
    con.close()


# 插入数据
def save_db2(con, cursor, data_dict):
    sql = 'insert into movie (title, releasetime, actor, ranks, score, cover) values (%s, %s, %s, %s, %s, %s)'
    print(sql)
    cursor.execute(sql, (
    data_dict['title'], data_dict['releasetime'], data_dict['actor'], data_dict['rank'], data_dict['score'],
    data_dict['cover']))
    con.commit()


# 疫情
def save_yq(con, cursor, data_dict):
    sql = 'insert into yiqing (date, xzs, new_cure, death) values (%s, %s, %s, %s)'
    print(sql)
    cursor.execute(sql, (data_dict['date'], data_dict['xzs'], data_dict['new_cure'], data_dict['death']))
    con.commit()
